package com.emep.changzhi.analyse.service.datamonitor.minute;

import com.datastax.driver.core.*;
import com.emep.changzhi.analyse.config.CassandraConfig;
import com.emep.changzhi.analyse.entity.datamonitor.minute.DataMinuteIndustrialGas;
import com.emep.changzhi.analyse.repository.datamonitor.minute.DataMinuteIndustrialGasRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author yangj
 * @version V1.0
 * @Description: 工业废气监测_分钟数据表业务层
 * @date 2018/2/27  16:11
 */
@Slf4j
@Service
public class DataMinuteIndustrialGasService {

    @Resource
    private DataMinuteIndustrialGasRepository dataMinuteIndustrialGasRepository;

    /**
     *
     * 新增
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public DataMinuteIndustrialGas save(Map<String, Object> payload) {
        DataMinuteIndustrialGas dataMinuteIndustrialGas = null;
        try {
            DataMinuteIndustrialGas dataMinuteIndustrialGas1 = (DataMinuteIndustrialGas) MapUtils.convertMap(DataMinuteIndustrialGas.class,payload);
            dataMinuteIndustrialGas = dataMinuteIndustrialGasRepository.save(dataMinuteIndustrialGas1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return dataMinuteIndustrialGas;
    }

    /**
     * 分页查询
     * */
//    public Map<String , Object> findNoQuery(Integer size , PagingState pagingState){
//        Map<String , Object> result = new HashMap<String , Object>();
//
//        Session session = null;
//        //定义一个Cluster类
//        Cluster cluster = Cluster.builder().addContactPoint("47.93.206.48").build();
//
//        //需要获取Session对象
//        session = cluster.connect();
//        Statement stmt = new SimpleStatement("select * FROM test.data_minute_industrial_gas");
//        stmt.setFetchSize(size);
//        //第一页没有分页状态
//        if(pagingState != null){
//            stmt.setPagingState(pagingState);
//        }
//        ResultSet rs = session.execute(stmt);
//        result.put("pagingState" , rs.getExecutionInfo().getPagingState());
//        System.out.println(rs.getAvailableWithoutFetching());
//        Iterator<Row> iter = rs.iterator();
//        while (rs.getAvailableWithoutFetching() == size && !rs.isFullyFetched()) {
//            rs.fetchMoreResults();
//            Row row = iter.next();
//            System.out.println(row);
//        }
//        return result;
//    }

    /**
     * @author xy
     * @Description: 首页
     * @date 2018/3/8 10:11
     * @version V1.0
     */
    public List<DataMinuteIndustrialGas> findDataMinuteIndustrialGasByHome(Integer pageSize){
        List<DataMinuteIndustrialGas> dataMinuteIndustrialGass = null;
        try {
            dataMinuteIndustrialGass = dataMinuteIndustrialGasRepository.dataMinuteIndustrialGasHome(pageSize);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return dataMinuteIndustrialGass;

    }
    /**
     * @author xy
     * @Description: 翻页查询
     * @date 2018/3/8 10:18
     * @version V1.0
     */

    public List<DataMinuteIndustrialGas> findDataMinuteIndustrialGasByFlip(String id,Integer pageSize) {
        List<DataMinuteIndustrialGas> dataMinuteIndustrialGass = null;
        try {
            dataMinuteIndustrialGass = dataMinuteIndustrialGasRepository.dataMinuteIndustrialGasFlip(id,pageSize);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return dataMinuteIndustrialGass;
    }
}
